package com.atguigu.gulimall.cart.controller;


import com.atguigu.gulimall.cart.service.CartService;
import com.atguigu.gulimall.cart.vo.Cart;
import com.atguigu.gulimall.cart.vo.CartItem;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;
import java.util.concurrent.ExecutionException;

@Controller
public class CartController {
    @Autowired
    CartService cartService;
    /**
     * auth:login
     * 添加购物车
     * RedirectAttributes ra
     *  ra.addFlashAttribute()  放在session里，只能取出一次
     *  ra.addAttribute() 放在url后面
     */
    @GetMapping("/addToCart")
    public String addToCart(@RequestParam("skuId") Long skuId, @RequestParam("num") Integer num, RedirectAttributes ra) throws ExecutionException, InterruptedException {
       CartItem cartItem = cartService.addToCart(skuId, num);
//        model.addAttribute("skuId",skuId);
        ra.addAttribute("skuId",skuId);
      return   "redirect:http://cart.gulimall.com/addToCartSuccess.html";
    }
    /**
     * 查询单个购物项
     * 添加到购物车成功后，重定向到此接口
     */
    @GetMapping("/addToCartSuccess.html")
    public String addToCartSuccessPage(@RequestParam("skuId") Long skuId, Model model) {
        CartItem cart = cartService.getCartItem(skuId);
        model.addAttribute("item", cart);
        return "success";
    }

    /**
     * 购物车列表页
     */
    @GetMapping("/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
//        UserInfo userInfo = CartInterceptor.threadLocal.get();
//        System.out.println(userInfo);
       Cart cart =  cartService.getCart();
       model.addAttribute("cart",cart);
        return "cartList";
    }
    @GetMapping("/checkItem")
    public String checkItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("check") Integer check
    ) {
        cartService.checkItem(skuId, check);

        return "redirect:http://cart.gulimall.com/cart.html";
    }
    @GetMapping("/countItem")
    public String countItem(@RequestParam("skuId") Long skuId,
                            @RequestParam("num") Integer count
    ) {
        cartService.countItem(skuId, count);

        return "redirect:http://cart.gulimall.com/cart.html";
    }
    @GetMapping("/deleteItem")
    public String countItem(@RequestParam("skuId") Long skuId
    ) {

        cartService.deleteItem(skuId);
        return "redirect:http://cart.gulimall.com/cart.html";
    }

    @GetMapping("/currentUserCartItems")
    @ResponseBody
    public List<CartItem> getCurrentUserCartItems(){
        List<CartItem> items = cartService.getUserCartItems();
        return items;
    }
}
